module App::View
    class MainWindow < KDE::MainWindow
        slots 'taskStopped()'
        slots 'taskRunning(QObject*, int)'

        attr_reader :reload_button, :toggl_web_button, :configure_parsers_button, :exit_button
        attr_reader :toolbar

        def initialize(toggl_tree_view, geo)
            super()
            
            toolbar = Qt::ToolBar.new
            @reload_button = toolbar.addAction KDE::Icon.new("mail-receive"), "Reload"
            @toggl_web_button = toolbar.addAction KDE::Icon.new("start-here"), "Toggl Web"
            @configure_parsers_button = toolbar.addAction KDE::Icon.new("preferences-other"), "Configure parsers"
            @exit_button = toolbar.addAction KDE::Icon.new("application-exit"), "Exit"

            hbox = Qt::HBoxLayout.new
            hbox.addWidget toolbar
            # For the 'plugins' popup menu
            @toolbar = toolbar
            # Qt::AlignRight
            #hbox.addWidget toggllink, 1, Qt::AlignLeft

            w = Qt::Widget.new(self)
            layout = Qt::VBoxLayout.new w
            layout.addLayout hbox
            layout.addWidget toggl_tree_view

            setWindowIcon Qt::Icon.new(File.dirname($0)+"/icons/karm.png")
            setWindowTitle "Ktoggl!"
            
            setGeometry geo.toRect if geo

            setCentralWidget w
        end

        def closeEvent(event)
            $widget.trayIconActivated
            event.ignore
        end

        private
        def taskRunning(runningtask, ticker)
            self.window_title = "#{runningtask.description}: #{ticker}s"
        end

        def taskStopped()
            self.window_title = "stopped"
        end
    end
end
